<?php
/**
 * Object Relational Persistence Mapping Class for table: rhm_kham_trong_mieng
 *
 * @package	modules.dept_pharmacy.ordo
 * @author	viethd
 * @version $
 * @copyright 2009 LHV Company
 */
class RhmKhamTrongMieng extends ORDataObject {

	/**#@+
	 * Fields of table: rhm_kham_trong_mieng mapped to class members
	 */
	var $rhm_kham_trong_mieng_id		= '';
	var $medical_record_id		= '';
	var $enum_tooth		= '';
	var $note		= '';
	var $status		= '';
	var $bridge		= '';
	
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'rhm_kham_trong_mieng';

	/**
	 * Primary Key
	 */
	var $_key = 'rhm_kham_trong_mieng_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'RhmKhamTrongMieng';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	
    public function setData($data = array()) {
        if (is_array($data)) {         
            $this->populateArray($data);
        }
    }

	public function register4Profile($recordId) {
	    $data = array();
	    for ($index1 = 1; $index1 < 53; $index1++) {    	    
			$data = array(
				'medical_record_id'	=> $recordId,
				'enum_tooth'		=> $index1,
				'status'	   		=> '000000000',
				'bridge'			=> '0'
			);
			$rhmKhamTrongMiengObj = Celini::newOrdo('RhmKhamTrongMieng');
			$rhmKhamTrongMiengObj->setData($data);
			$rhmKhamTrongMiengObj->persist();
	    }	   
	}
	
	public function getUpperAdultTeeth($recordId) {
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "rhm_kham_trong_mieng_id, enum_tooth, status, note, bridge",
			'from'		=> "$this->_table",
			'where'		=> "medical_record_id = $recordId AND enum_tooth < 17"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function getLowerAdultTeeth($recordId) {
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "rhm_kham_trong_mieng_id, enum_tooth, status, note, bridge",
			'from'		=> "$this->_table",
			'where'		=> "medical_record_id = $recordId AND enum_tooth > 16 AND enum_tooth < 33"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function getUpperChildTeeth($recordId) {
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "rhm_kham_trong_mieng_id, enum_tooth, status, note, bridge",
			'from'		=> "$this->_table",
			'where'		=> "medical_record_id = $recordId AND enum_tooth > 32 AND enum_tooth < 43"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function getLowerChildTeeth($recordId) {
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "rhm_kham_trong_mieng_id, enum_tooth, status, note, bridge",
			'from'		=> "$this->_table",
			'where'		=> "medical_record_id = $recordId AND enum_tooth > 42"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function setToothBridge($teethId, $value) {
		$sql = "UPDATE $this->_table SET bridge = $value WHERE $this->_key IN ( $teethId )";	
		$this->_execute($sql);
	}
	
	public function setToothStatus($toothId, $value) {
        $sql = "UPDATE $this->_table SET status = '$value' WHERE $this->_key = $toothId";
		$this->_execute($sql);
	}
	
	public function setNote($toothId, $note) {
		$note = Utils::sanitizeText($note);
		$sql = "UPDATE $this->_table SET note = '$note' WHERE $this->_key = $toothId";
		$this->_execute($sql);
		return $note;
	}
}
?>